package com.sanjishu.enterlab.electricity.sale.controller.manage;

import com.sanjishu.enterlab.electricity.sale.controller.Response;
import com.sanjishu.enterlab.electricity.sale.domain.ContractData;
import com.sanjishu.enterlab.electricity.sale.domain.ContractFile;
import com.sanjishu.enterlab.electricity.sale.domain.ContractInfo;
import com.sanjishu.enterlab.electricity.sale.service.ContractDataService;
import com.sanjishu.enterlab.electricity.sale.service.ContractFileService;
import com.sanjishu.enterlab.electricity.sale.service.ContractInfoService;
import com.sanjishu.enterlab.electricity.sale.service.PdfService;
import com.sanjishu.enterlab.electricity.sale.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("manage/contractInfo")
public class ContractManageController {

    @Autowired
    private ContractDataService contractDataService;
    @Autowired
    private ContractInfoService contractInfoService;
    @Autowired
    private PdfService pdfService;
    @Autowired
    private ContractFileService contractFileService;

    @RequestMapping(value = "showContractData", method = RequestMethod.GET)
    public String showContractData(Integer contractInfoId, Model model) {
        ContractInfo contractInfo = contractInfoService.findById(contractInfoId);
        HashMap<String, ContractData> contractDataHashMap = contractDataService.findByContractInfoId(contractInfoId);
        model.addAttribute("contractInfo", contractInfo);
        model.addAttribute("contractDataHashMap", contractDataHashMap);
        return "manage/contracts/contractData";
    }

    @RequestMapping(value = "pass", method = RequestMethod.POST)
    public String pass(Integer id, Integer contractId, Integer types, Model model) {
        contractDataService.passData(id, contractId, types);
        return showContractData(contractId, model);
    }

    @RequestMapping(value = "refuse", method = RequestMethod.POST)
    public String refuse(Integer id, Integer contractId, Integer types, Model model) {
        contractDataService.refuseData(id, contractId, types);
        return showContractData(contractId, model);
    }

    @RequestMapping(value = "generatePdf", method = RequestMethod.POST)
    @ResponseBody
    public Response generatePdf(Integer contractId, Model model) throws Exception {
        return pdfService.generalContract(contractId);
    }

    @RequestMapping(value = "userFile", method = RequestMethod.GET)
    public String userFile(Integer contractId, Model model) {
        ContractInfo contractInfo = contractInfoService.findById(contractId);
        List<ContractFile> fileList = contractFileService.getByContractId(contractId);
        model.addAttribute("contractInfo", contractInfo);
        model.addAttribute("dataList", fileList);
        return "manage/contracts/contractFile";
    }

    @RequestMapping(value = "deleteFile", method = RequestMethod.POST)
    @ResponseBody
    public Response deleteFile(Integer fileId, Model model) {
        contractFileService.deleteFile(fileId);
        return Response.buildSuccess("删除成功。");
    }

    @RequestMapping(value = "fileUpload", method = RequestMethod.POST)
    @ResponseBody
    public Response fileUpload(@RequestParam("file") MultipartFile file,
                               String name, Integer contractInfoId, Model model) throws Exception {
        contractFileService.fileUpload(file, name, contractInfoId);
        return Response.buildSuccess("添加成功。");
    }

    @RequestMapping(value = "sureSign", method = RequestMethod.POST)
    @ResponseBody
    public Response sureSign(Integer contractId, Model model) throws Exception {
        return contractInfoService.sureSigned(contractId);
    }

    @RequestMapping(value = "boundContract", method = RequestMethod.POST)
    @ResponseBody
    public Response boundContract(Integer contractInfoId, Model model) {
        contractInfoService.updateBoundStatus(contractInfoId, ContractInfo.BOUND_STATUS_ALREADY);
        return Response.buildSuccess("绑定成功。");
    }

    @RequestMapping(value = "noBound", method = RequestMethod.POST)
    @ResponseBody
    public Response noBound(Integer contractInfoId, Model model) {
        contractInfoService.updateBoundStatus(contractInfoId, ContractInfo.BOUND_STATUS_NO);
        return Response.buildSuccess("争议未绑定成功。");
    }

    @RequestMapping(value = "neverBound", method = RequestMethod.POST)
    @ResponseBody
    public Response neverBound(Integer contractInfoId, Model model) {
        contractInfoService.updateBoundStatus(contractInfoId, ContractInfo.BOUND_STATUS_NEVER);
        return Response.buildSuccess("已修改为新增量。");
    }

    @RequestMapping(value = "boundList", method = RequestMethod.GET)
    public String boundList(Integer page, Model model) {
        Page<ContractInfo> contractInfoPage = contractInfoService.listByBoundStatus(ContractInfo.BOUND_STATUS_ALREADY, 1);
        model.addAttribute("pageData", contractInfoPage);
        return "manage/contracts/list";
    }

    @RequestMapping(value = "noBoundList", method = RequestMethod.GET)
    public String noBoundList(Integer page, Model model) {
        Page<ContractInfo> contractInfoPage = contractInfoService.listByBoundStatus(ContractInfo.BOUND_STATUS_NO, 1);
        model.addAttribute("pageData", contractInfoPage);
        return "manage/contracts/list";
    }

    @RequestMapping(value = "neverBoundList", method = RequestMethod.GET)
    public String neverBoundList(Integer page, Model model) {
        Page<ContractInfo> contractInfoPage = contractInfoService.listByBoundStatus(ContractInfo.BOUND_STATUS_NEVER, 1);
        model.addAttribute("pageData", contractInfoPage);
        return "manage/contracts/list";
    }

}
